package calculate_server

import (
	"context"
	"google.golang.org/grpc"
	common "jihe.common"
	"jihe.common/protos/calculate_server/proto"
	"strings"
	"time"
)

var once = false
var conns []proto.CalculateClient

func GetClient() []proto.CalculateClient {
	if !once {
		for i := 0; i < len(common.Configer.Grpc.CalculateServers); i++ {
			grpcConn, err := grpc.Dial(common.Configer.Grpc.CalculateServers[i], grpc.WithInsecure())
			if err != nil {
				return conns
			}
			conns = append(conns, proto.NewCalculateClient(grpcConn))
		}
		once = true
	}
	return conns
}

func CalculateCreateCutpartQueue(c context.Context, in *proto.CalculateCutpartQueues) (out *proto.CalculateNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateCreateCutpartQueue(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalculateFashion(c context.Context, in *proto.CalculateFashionReq) (out *proto.CalculateFashionRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateFashion(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalculateGetCutpartQueueByNeedId(c context.Context, in *proto.CalculateCutpartQueue) (out *proto.CalculateCutpartQueues, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateGetCutpartQueueByNeedId(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalculateGetNextCutpartByWait(c context.Context, in *proto.CalculateNull) (out *proto.CalculateCutpartQueue, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateGetNextCutpartByWait(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalculateUpdateCompleteById(c context.Context, in *proto.CalculateCutpartQueue) (out *proto.CalculateNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateUpdateCompleteById(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalculateBegin(c context.Context, in *proto.CalculateCutpartQueue) (out *proto.CalculateNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateBegin(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalculateUpdateFail(c context.Context, in *proto.CalculateCutpartQueue) (out *proto.CalculateNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateUpdateFail(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalculateDeleteByNeedId(c context.Context, in *proto.CalculateCutpartQueue) (out *proto.CalculateNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateDeleteByNeedId(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalculateLineupNumByMinId(c context.Context, in *proto.CalculateLineupNumByMinIdReq) (out *proto.CalculateLineupNumByMinIdRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateLineupNumByMinId(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalculateAbort(c context.Context, in *proto.CalculateCutpartQueue) (out *proto.CalculateNull, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalculateAbort(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func GetBorderNode(c context.Context, in *proto.GetBorderNodeReq) (out *proto.GetBorderNodeRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].GetBorderNode(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func GetDxfInfo(c context.Context, in *proto.GetDxfInfoReq) (out *proto.GetDxfInfoRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].GetDxfInfo(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}

func CalibrationUv(c context.Context, in *proto.CalibrationUvReq) (out *proto.CalibrationUvRes, err error) {
	clients := GetClient()
	num := time.Now().UnixNano()
	for i := 0; i < len(clients); i++ {
		index := (num + int64(i)) % int64(len(clients))
		out, err = clients[index].CalibrationUv(c, in)
		if err != nil && strings.Contains(err.Error(), "connection refused") {
			//fmt.Println(err.Error())
		} else {
			return
		}
	}
	return
}
